﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Business;
using Core.Entity;

namespace TicketManagement.Staff
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.authenticate();

            if (!IsPostBack)
                loadData();
        }

        private void authenticate()
        {
            if (Session["User"] != null)
            {
                UserBus userBus = new UserBus();
                Core.Entity.User curUser = userBus.get(Convert.ToInt32(Session["User"]));
                if (curUser.UserTypeId == 4)
                {
                    // Do not allow customer here
                    Response.Redirect("/Home/Index.aspx", true);
                }
                else
                {
                    if (curUser.UserTypeId != 1)
                    {
                        // Only allow admin
                        Response.Redirect("/Home/Dashboard.aspx", true);
                    }
                }
            }
        }

        public void loadData()
        {
            StaffBus bus = new StaffBus();
            string code = txtCode.Text.Trim();
            string fullName = txtFullName.Text.Trim();
            string birthDay = txtBirthDay.Text.Trim();
            string hireDate = txtHireDay.Text.Trim();

            string orderBy = "order by STF_CODE";
            if (ViewState["sortExpr"] != null)
            {
                orderBy = "order by " + ViewState["sortExpr"] + " " + GetSortDirection();
            }

            listStaff.DataSource = bus.filter(code, fullName, birthDay, hireDate, orderBy);
            listStaff.DataBind();
        }

        protected void listStaff_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            listStaff.PageIndex = e.NewPageIndex;
            loadData();
        }

        private string GridViewSortDirection
        {
            get { return ViewState["SortDirection"] as string ?? "DESC"; }
            set { ViewState["SortDirection"] = value; }
        }
        private string GetSortDirection()
        {
            switch (GridViewSortDirection)
            {
                case "ASC": GridViewSortDirection = "DESC"; break;
                case "DESC": GridViewSortDirection = "ASC"; break;
            }
            return GridViewSortDirection;
        }

        protected void listStaff_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["sortExpr"] = e.SortExpression;
            loadData();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit.aspx", true);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            StaffBus bus = new StaffBus();
            string strArrayID = "";
            foreach (GridViewRow rows in listStaff.Rows)
            {
                CheckBox chk = (CheckBox)rows.FindControl("cbxDelId");

                if (chk.Checked)
                {
                    strArrayID += "'" + ((Label)rows.FindControl("lblStaffId")).Text.ToString() + "',";
                }
            }
            if (strArrayID != "")
            {
                if (bus.delete(strArrayID.Substring(0, strArrayID.LastIndexOf(","))))
                {
                    this.loadData();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.loadData();
        }
    }
}